package com.example.module_filemanage.Adapter;

/**
 * Created by SJ on 2018/4/26.
 */

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;

import com.example.module_filemanage.Control.SearchView;
import com.example.module_filemanage.Interface.IFileInfoOnclickListener;
import com.example.module_filemanage.MVVM.Module.Entil.Item_FileInfo_module;
import com.example.module_filemanage.R;
import com.example.module_filemanage.databinding.FmItemMain2Binding;
import com.example.module_filemanage.databinding.FmItemMainBinding;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Suggestions Adapter.
 *
 * @author Miguel Catalan Bañuls
 */
public class SearchFilter_Adapter extends RecyclerView.Adapter<SearchFilter_Adapter.ViewHolder> implements Filterable {

    private ArrayList<Item_FileInfo_module> data;
    private List<Item_FileInfo_module> suggestions;
    private Drawable suggestionIcon;
    private LayoutInflater inflater;
    private boolean ellipsize;
    private IFileInfoOnclickListener mIFileInfoOnclickListener;
    private SearchView.Flag back;
    public SearchFilter_Adapter(Context context, List<Item_FileInfo_module> suggestions) {
        inflater = LayoutInflater.from(context);
        data = new ArrayList<>();
        this.suggestions = suggestions;
    }

    public SearchFilter_Adapter(Context context, List<Item_FileInfo_module> suggestions, Drawable suggestionIcon, boolean ellipsize, IFileInfoOnclickListener onclicklistener, SearchView.Flag back) {
        inflater = LayoutInflater.from(context);
        data = new ArrayList<>();
        this.suggestions = suggestions;
        this.suggestionIcon = suggestionIcon;
        this.ellipsize = ellipsize;
        this.mIFileInfoOnclickListener=onclicklistener;
        this.back=back;
    }

    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();
                if (!TextUtils.isEmpty(constraint)) {

                    // Retrieve the autocomplete results.
                    List<Item_FileInfo_module> searchData = new ArrayList<>();

                    for (Item_FileInfo_module item : suggestions) {
                        if (item.filename.toLowerCase().contains(constraint.toString().toLowerCase())) {
                            searchData.add(item);
                        }
                    }

                    // Assign the data to the FilterResults
                    filterResults.values = searchData;
                    filterResults.count = searchData.size();
                }
                return filterResults;
            }
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                if (results.values != null && results.count>0) {
                    data = (ArrayList<Item_FileInfo_module>) results.values;
                    Collections.sort(data,new Comparator<Item_FileInfo_module>(){
                        public int compare(Item_FileInfo_module arg0, Item_FileInfo_module arg1) {
                            return -arg0.filetime.compareTo(arg1.filetime);
                        }
                    });
                    notifyDataSetChanged();
                }
            }
        };
        return filter;
    }


    @Override
    public int getItemViewType(int position) {
        if (data.get(position).filename.matches(".+\\.(jpg|jpeg|png|gif|JPG|JPEG|PNG|GIF)")){
            return 0;
        }else {
            return 1;
        }

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewDataBinding _dataBinding=null;
        switch (viewType){
            case 0:
                _dataBinding=DataBindingUtil.inflate(inflater,R.layout.fm_item_main2,parent,false);
                break;
            case 1:
                _dataBinding=DataBindingUtil.inflate(inflater,R.layout.fm_item_main,parent,false);
                break;
        }
        return new ViewHolder(_dataBinding);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (holder.getdataBinding() instanceof FmItemMain2Binding){
            FmItemMain2Binding _binding= (FmItemMain2Binding) holder.getdataBinding();
            _binding.setItem((Item_FileInfo_module)getItem(position));
            _binding.frameMainitem.setBackgroundColor(Color.WHITE);
            _binding.tvFilename.setTextColor(Color.BLACK);
            _binding.tvFilesize.setTextColor(Color.BLACK);
            _binding.tvFiletime.setTextColor(Color.BLACK);
            _binding.imageView4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View IView) {
                    mIFileInfoOnclickListener.showimgListener(IView,position);
                }
            });
            _binding.imgDel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View IView) {
                    mIFileInfoOnclickListener.deletListener(IView,position,(Item_FileInfo_module)getItem(position));
                }
            });
            _binding.imgDownload.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View IView) {
                    mIFileInfoOnclickListener.downloadListener(IView,position);
                }
            });
            _binding.frameMainitem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    back.enter((Item_FileInfo_module)getItem(position));
                }
            });
            _binding.frameMainitem.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View IView) {
                    back.enter((Item_FileInfo_module)getItem(position));
                    mIFileInfoOnclickListener.onclickListener(IView,position);
                    return false;
                }
            });
        }else{
            FmItemMainBinding _binding= (FmItemMainBinding) holder.getdataBinding();
            _binding.setItem((Item_FileInfo_module)getItem(position));
            _binding.frameMainitem.setBackgroundColor(Color.WHITE);
            _binding.tvFilename.setTextColor(Color.BLACK);
            _binding.tvFilesize.setTextColor(Color.BLACK);
            _binding.tvFiletime.setTextColor(Color.BLACK);
            _binding.imgDel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View IView) {
                    mIFileInfoOnclickListener.deletListener(IView,position,(Item_FileInfo_module)getItem(position));
                }
            });
            _binding.imgDownload.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View IView) {
                    mIFileInfoOnclickListener.downloadListener(IView,position);
                }
            });
            _binding.frameMainitem.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View IView) {
                    back.enter((Item_FileInfo_module)getItem(position));
                    mIFileInfoOnclickListener.onclickListener(IView,position);
                    return false;
                }
            });
            _binding.frameMainitem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    back.enter((Item_FileInfo_module)getItem(position));
                }
            });
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
    public static class ViewHolder extends RecyclerView.ViewHolder{
        ViewDataBinding _dataBinding=null;
        public ViewHolder(ViewDataBinding itemView) {
            super(itemView.getRoot());
            this._dataBinding=itemView;
        }

        public ViewDataBinding getdataBinding() {
            return _dataBinding;
        }
    }

    public void removeAt(int position){
        data.remove(position);
        notifyDataSetChanged();
    }
    public void changespace(int position,String key){
        Item_FileInfo_module _item_fileInfo_module=data.get(0);
        _item_fileInfo_module.setFilename(key);
        data.set(0,_item_fileInfo_module);
        notifyDataSetChanged();
    }
    public Object getItem(int position) {
        return data.get(position);
    }
    public int getCount(){
        return data.size();
    }

}
